// pages/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nextPageUrl: true,
    pageNum: 1,
    positions: [],
    needRec:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    //获得dialog组件
    this.loginwin = this.selectComponent("#loginwin");
    this.showDialog(function(res){
        if(res!==false){
          if (options.scene) {
            var scene = decodeURIComponent(options.scene);
            var id = scene.split('&')[0].split('=')[1];
            var keyVal = scene.split('&')[1].split('=')[1];
            app.apiPost(app.apiList.getScanLog, {
              position_id: id,
              key: keyVal
            }, function (res) {
              if (res.code == 200) {
                console.log('日志纪录成功')
                wx.navigateTo({
                  url: '/pages/detail/detail?id=' + id,
                })
              }
            })

            //return false;
          };
        }
    });
  },
  showDialog: function(fun) {
    this.loginwin.showDialog(fun);
  },

  loginedGetUserInfo: function(res) {
    console.log('loginevent?');
    var city = app.globalData.city;
    // 用户点击授权后，这里可以做一些登陆操作
    if (res) {
      var that = this;
      //收集设备信息
      app.util.checkSystemInfo();
      that.setData({
        city: city
      })
      that.getIndexPositions(city, that.data.pageNum, function() {});
    }
  },
  //获取职位列表
  getIndexPositions: function(city, page, callback) {
    var that = this;
    app.apiGet(app.apiList.index, {
      page: page,
      city: city
    }, function(res) {
      if (res.code == 200) {
        var positions = res.data.positions;
        var tmp = [];
        if(positions.length>0){
          for(var i=0;i<positions.length;i++){
            positions[i].inCity = positions[i].address.indexOf(city)>=0?true:false;
          }
        }
        let newpositions = that.data.positions.concat(positions);
        let newpageNum = ++that.data.pageNum;
        if (res.data.positions[0].address.indexOf(city) < 0 && page==1&&city!='全国'){
          setTimeout(function () {
            wx.showModal({
              title: '提示',
              content: '当前城市暂无招聘岗位，更多(高端)职位请联系工作人员（微信）：13263336097',
              showCancel: false
            })
            
          }, 900)
          that.setData({
            needRec: false
          });
        }
        
        that.setData({
          positions: newpositions,
          nextPageUrl: res.data.next_page_url,
          pageNum: newpageNum
        })
      }
      callback();
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.data.positions = [];
    this.data.pageNum = 1;
    this.getIndexPositions(this.data.city, this.data.pageNum, function() {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

    if (this.data.nextPageUrl) {
      this.getIndexPositions(this.data.city, this.data.pageNum, function() {})
    }

  },
  //选择城市
  cityTap: function () {
    wx.navigateTo({
      url: '/pages/switchCity/switchCity?isindex=1&city=' + this.data.city,
    })
  },
  //进入搜索页面
  searchTap: function() {
    var url = '/pages/search/search?city=' + app.globalData.city
    wx.navigateTo({
      url: url,
    })
  },
  //进入详情页面
  positionDetailTap: function(e) {
    var id = e.currentTarget.dataset.positionId;
    if (id) {
      wx.navigateTo({
        url: '/pages/detail/detail?id=' + id,
      })
    }

  },
  //share 统一分享
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '财咖｜会计招聘找工作',
      path: '/pages/index/index',
      imageUrl: '/images/share.jpg',
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }

})